package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AInfor;
import com.ruoyi.system.service.IAInforService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 分享类别Controller
 *
 * @author gf
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/system/aInfor")
public class AInforController extends BaseController {
    @Autowired
    private IAInforService aInforService;

    /**
     * 查询分享类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:aInfor:list')")
    @GetMapping("/list")
    public AjaxResult list(AInfor aInfor) {
        List<AInfor> list = aInforService.selectAInforList(aInfor);
        return success(list);
    }

    /**
     * 导出分享类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:aInfor:export')")
    @Log(title = "分享类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AInfor aInfor) {
        List<AInfor> list = aInforService.selectAInforList(aInfor);
        ExcelUtil<AInfor> util = new ExcelUtil<>(AInfor.class);
        util.exportExcel(response, list, "分享类别数据");
    }

    /**
     * 获取分享类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:aInfor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(aInforService.selectAInforById(id));
    }

    /**
     * 新增分享类别
     */
    @PreAuthorize("@ss.hasPermi('system:aInfor:add')")
    @Log(title = "分享类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AInfor aInfor) {
        if (!aInforService.checkNameUnique(aInfor)) {
            return error("新增分类'" + aInfor.getInName() + "'失败，分类名称已存在");
        }
        return toAjax(aInforService.insertAInfor(aInfor));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('system:aInfor:edit')")
    @Log(title = "分享类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AInfor aInfor) {
        Long id = aInfor.getId();
        if (!aInforService.checkNameUnique(aInfor)) {
            return error("修改分类'" + aInfor.getInName() + "'失败，分类名称已存在");
        }
        else if (aInfor.getParentId().equals(id)) {
            return error("修改分类'" + aInfor.getInName() + "'失败，上级分类不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, aInfor.getIsShow()) && aInforService.selectNormalChildrenCategoryById(id) > 0) {
            return error("该分类包含未停用的子分类！");
        }
        aInfor.setUpdateBy(getUsername());
        return toAjax(aInforService.updateAInfor(aInfor));
    }

    /**
     * 删除分享类别
     */
    @PreAuthorize("@ss.hasPermi('system:aInfor:remove')")
    @Log(title = "分享类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/logicDel/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        AInfor aInfor = new AInfor();
        aInfor.setParentId(id);
        aInfor.setDelFlag("0");
        List<AInfor> categories = aInforService.selectAInforList(aInfor);
        if(CollectionUtils.isNotEmpty(categories)){
            return error("存在下级分类，不允许删除");
        }
        //todo 是否已使用
        /*GfXxx xxx = new GfXxx();
        xxx.setCategoryId(id);
        List<GfXxx> xxxList = gfXxxService.selectGfXxxList(xxx);
        if(CollectionUtils.isNotEmpty(xxxList)){
            return error("分类已使用，不允许删除");
        }*/
        AInfor category = aInforService.selectAInforById(id);
        category.setDelFlag("2");
        return toAjax(aInforService.updateAInfor(category));
    }

}
